package logic

import (
	"context"

	"friend/internal/svc"
	"friend/pb/friend"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGroupListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGroupListLogic {
	return &GetGroupListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// GetGroupList 好友分组列表
func (l *GetGroupListLogic) GetGroupList(in *friend.GroupListRequest) (*friend.GroupListResponse, error) {
	total, err := l.svcCtx.FriendGroupExtendModel.Count(l.ctx, in.UserId)
	if err != nil {
		return nil, err
	}
	list, err := l.svcCtx.FriendGroupExtendModel.FindList(l.ctx, in.UserId, in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}

	data := make([]*friend.GroupInfo, 0)
	for _, v := range list {
		data = append(data, &friend.GroupInfo{Id: v.Id, Name: v.Name, Sort: v.Sort})
	}
	return &friend.GroupListResponse{Total: total, Data: data}, nil
}
